Dreamweaver Article

How to Develop an Online Shopping Cart (Part Three)


Table of Contents

  1. Introduction
  2. Example Application Setup and the Database
  3. Application.cfm
  4. The Home Page
  5. The Check Out Page
  6. The Process Order Page
  7. The Administration Page

Setting Up the Sample Files

Installing ColdFusion MX is optional and unnecessary for deconstructing the shopping cart in this article. However, you will need it if you want to run the example application. If you do not plan to install ColdFusion and run the example application, you can still see a hosted version of the application running here. If you do not plan to run the shopping cart application locally, you will still need to download and unzip the sample files linked from the beginning of this article to be able to follow along. If you do not plan to run the shopping cart application locally, you can skip to the next page of the article.

Note: Again, the example application in the download files is not meant to be a production-ready shopping cart and does not include the SSL security described in parts one and two of this article series.

If you plan to run the example application locally and have not already done so, download the example application in the sample files linked from the beginning of this article. Keep in mind that you need to have ColdFusion MX installed to run this example application. You can get a trial version of ColdFusion MX from using the link at the beginning of this article. Installing ColdFusion is beyond the scope of this article. Consult the ColdFusion Support Center for troubleshooting the installation of ColdFusion MX. Use the following steps to install the example application.

  1. If you plan to run the example application and have installed ColdFusion MX, create a folder called cart in the wwwroot directory under the CfusionMX directory (C:\CFusionMX\wwwroot\cart). This assumes you are using the default JWS web server installation and not the installation for a third-party web server such as IIS or Apache.
  2. Next, unzip the example files into your new cart folder.
  3. Open Dreamweaver MX and create a new site and map the new site to the Example files folder.
  4. Finally, if you are running CFMX, create a Data Source Name called "DevNetExample" in ColdFusion Administrator and browse to the Access database file in you site (See ColdFusion MX documentation if you are not familiar with how to do this.).

The Database

As I mentioned in parts one and two of this article series, the database is the most critical component of an e-commerce application. Once you have designed and populated database, you will have customer and order records—possibly thousands of them. Changing the database structure is extremely difficult once the database has data in it. Even though the sample database is quite simple, you still need to ensure that you keep data and referential integrity in mind and build a database that can be scaled so that it will stand the test of time.

The example application uses a Microsoft Access database. Even if you are going to deploy to a more robust database such as SQL Server, Access is a great tool for mocking up and testing a database.

Note: I'm going to assume you are somewhat familiar with Microsoft Access. If you aren't, you may want to read some more and get acquainted with it. If you are planning on developing dynamic web applications on the Microsoft Windows platform, doing so will be well worth your effort. If you plan to develop applications using databases (such as MySQL) on non-windows platforms, you will still be able to learn from the below database description. However, some things such as datatype names of the database table fields may differ. Also, the screenshots below only apply to Access. Even if you are primarily a web designer, possessing a basic knowledge of how databases function will go a long way in improving your ability to deliver quality dynamic applications.

Here is a general overview of the tables and fields in the database and the relationships between them.

The database

Figure 1. The database

(+) View larger

I will guide you through each of the database tables, review the fields and data types, and explain how they work in the application.

Administration Users Table:

The Administration Users table stores the usernames and passwords that your app will use to gain access to the application's administrative back end. Here is what it contains.

Admin Table:

Field Name

Data Type

Field Size

Other Parameters

AdminID

AutoNumber

Long Integer

Key Field

uName

Text

100

Leave Default

uPassword

Text

50

Indexed: Yes (No Duplicates)

In the Access Design View the table looks like the following:

The AdminUsers table in Access Design View

Figure 2. The AdminUsers table in Access Design View

Notice the key icon next to the AdminID. This shows that this field is the unique index for this table. Each table must have a unique primary key field to identify records. To set the primary key field in Access, click the name of the field (in this case AdminID). Next, right click Select Primary Key from the pop-up menu. To set the data type parameters, click the Field Name you wish to edit. Choose the Name field this time. As shown in Figure 3 below, you'll notice that the bottom of the Design view window has a window tab named General.

The Name field parameters

Figure 3. The Name field parameters

This is where you set the data type parameters. For the Name field, leave all the options in the default setting except for the Field Size. Set the Field Size parameter to 100. This means the Name field will hold up to 100 characters.

Items Table

The Items table stores the items that are for sale. The Items table holds the following data: the name of the item, a memo field to store the description, the price, and the name of the image file. You'll notice that the example database only stores the name of the image. It's never a good idea to store the directory path to the images in the database ( …/images/imagename.gif). If you need to move the image files later, all of the data in your image fields becomes invalid and you'll have to update all your records. To avoid this, always have the directory path to the images in your application code where you can more easily update it.

Items Table:

Field Name

Data Type

Field Size

Other Parameters

ItemID

AutoNumber

Long Integer

Key Field

ItemName

Text

100

Leave Default

ItemDiscrip

Memo

 

Leave Default

ItemPrice

Currency

 

Leave Default

ItemImage

Text

50

Leave Default

Customer Table

The customer information is stored in the Customer table. You'll notice that I'm not storing the customer data and the order data together in the same table. The first rule of good database design is to eliminate duplicate data. Storing the customer information with the order information would result in a lot of duplicate data if the same customer had several orders.

Customer Table:

Field Name

Data Type

Field Size

Other Parameters

custID

AutoNumber

Long Integer

Key Field

custName

Text

150

Leave Default

custAddress

Text

150

Leave Default

custCity

Text

50

Leave Default

custState

Text

50

Leave Default

custZip

Text

25

Leave Default

Storing Orders

Next, you need a place to store the orders. This presents a challenge. If the customer orders more than one product, you could create a line for each item, but this would result in a lot of duplicate data if you repeated the order ID, date, and total on each line. To avoid this situation, write the order data to two related tables. One table will store data that is unique to each order: order ID, the customer ID, the amount of the order, and the order date. The second table will store the items ordered.

Orders Table:

Field Name

Data Type

Field Size

Other Parameters

orderID

AutoNumber

Long Integer

Key Field

orderCustID

Number

Long Integer

Leave Default

orderTotal

Currency

 

Leave Default

orderDate

Date/Time

ShortDate

Now()

Next, create the Order Items table to store the items ordered.

OrdersItems Table:

Field Name

Data Type

Field Size

Other Parameters

orditmID

AutoNumber

Long Integer

Key Field

orditmOrderID

Number

Long Integer

Leave Default

orditmItemID

Number

Long Integer

Leave Default

orditmQty

Number

Long Integer

Leave Default

Data Relationships

Look at Figure 1 above and notice the relationships between the tables. These are depicted by the lines between the tables. The Customer ID relates the Customer table to the Orders table. This allows your shopping cart application to know what orders belong to a given customer. The OrderID relates the OrderItems Table to the Orders table. Finally, the ItemsID relates the OrderItems table to the Items table.

These relationships allow your shopping cart application to keep track of what customers have ordered and what items were in each order. You can even enforce referential integrity so a customer record cannot be deleted if there is an order record associated with it. This will keep the application from creating orphan records, which are orders that have no Customer related to them.

Next, we'll take a look at the application files. I'll highlight the main points in the code to show what is going on, but I recommend that you take some time to look over the files carefully in the Dreamweaver Code view. I've commented the code quite extensively to document what it is going on.